﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZY.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace ZY.SQLServerDAL
{
	/// <summary>
	/// 数据访问类:Tab_Power
	/// </summary>
	public partial class Tab_Power:ITab_Power
	{
		public Tab_Power()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(decimal po_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Tab_Power");
			strSql.Append(" where po_id=@po_id");
			SqlParameter[] parameters = {
					new SqlParameter("@po_id", SqlDbType.Decimal)
			};
			parameters[0].Value = po_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public decimal Add(ZY.Model.Tab_Power model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Tab_Power(");
			strSql.Append("po_name,po_type,po_ico,po_state,po_src,po_isroot,po_upid,po_note,po_createtime,po_lastime,po_sn,po_ext1,po_ext2,po_ext3)");
			strSql.Append(" values (");
			strSql.Append("@po_name,@po_type,@po_ico,@po_state,@po_src,@po_isroot,@po_upid,@po_note,@po_createtime,@po_lastime,@po_sn,@po_ext1,@po_ext2,@po_ext3)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@po_name", SqlDbType.VarChar,50),
					new SqlParameter("@po_type", SqlDbType.Int,4),
					new SqlParameter("@po_ico", SqlDbType.VarChar,500),
					new SqlParameter("@po_state", SqlDbType.Int,4),
					new SqlParameter("@po_src", SqlDbType.VarChar,200),
					new SqlParameter("@po_isroot", SqlDbType.Int,4),
					new SqlParameter("@po_upid", SqlDbType.Int,4),
					new SqlParameter("@po_note", SqlDbType.VarChar,500),
					new SqlParameter("@po_createtime", SqlDbType.DateTime),
					new SqlParameter("@po_lastime", SqlDbType.DateTime),
					new SqlParameter("@po_sn", SqlDbType.Int,4),
					new SqlParameter("@po_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@po_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@po_ext3", SqlDbType.VarChar,50)};
			parameters[0].Value = model.po_name;
			parameters[1].Value = model.po_type;
			parameters[2].Value = model.po_ico;
			parameters[3].Value = model.po_state;
			parameters[4].Value = model.po_src;
			parameters[5].Value = model.po_isroot;
			parameters[6].Value = model.po_upid;
			parameters[7].Value = model.po_note;
			parameters[8].Value = model.po_createtime;
			parameters[9].Value = model.po_lastime;
			parameters[10].Value = model.po_sn;
			parameters[11].Value = model.po_ext1;
			parameters[12].Value = model.po_ext2;
			parameters[13].Value = model.po_ext3;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToDecimal(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ZY.Model.Tab_Power model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Tab_Power set ");
			strSql.Append("po_name=@po_name,");
			strSql.Append("po_type=@po_type,");
			strSql.Append("po_ico=@po_ico,");
			strSql.Append("po_state=@po_state,");
			strSql.Append("po_src=@po_src,");
			strSql.Append("po_isroot=@po_isroot,");
			strSql.Append("po_upid=@po_upid,");
			strSql.Append("po_note=@po_note,");
			strSql.Append("po_createtime=@po_createtime,");
			strSql.Append("po_lastime=@po_lastime,");
			strSql.Append("po_sn=@po_sn,");
			strSql.Append("po_ext1=@po_ext1,");
			strSql.Append("po_ext2=@po_ext2,");
			strSql.Append("po_ext3=@po_ext3");
			strSql.Append(" where po_id=@po_id");
			SqlParameter[] parameters = {
					new SqlParameter("@po_name", SqlDbType.VarChar,50),
					new SqlParameter("@po_type", SqlDbType.Int,4),
					new SqlParameter("@po_ico", SqlDbType.VarChar,500),
					new SqlParameter("@po_state", SqlDbType.Int,4),
					new SqlParameter("@po_src", SqlDbType.VarChar,200),
					new SqlParameter("@po_isroot", SqlDbType.Int,4),
					new SqlParameter("@po_upid", SqlDbType.Int,4),
					new SqlParameter("@po_note", SqlDbType.VarChar,500),
					new SqlParameter("@po_createtime", SqlDbType.DateTime),
					new SqlParameter("@po_lastime", SqlDbType.DateTime),
					new SqlParameter("@po_sn", SqlDbType.Int,4),
					new SqlParameter("@po_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@po_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@po_ext3", SqlDbType.VarChar,50),
					new SqlParameter("@po_id", SqlDbType.Decimal,9)};
			parameters[0].Value = model.po_name;
			parameters[1].Value = model.po_type;
			parameters[2].Value = model.po_ico;
			parameters[3].Value = model.po_state;
			parameters[4].Value = model.po_src;
			parameters[5].Value = model.po_isroot;
			parameters[6].Value = model.po_upid;
			parameters[7].Value = model.po_note;
			parameters[8].Value = model.po_createtime;
			parameters[9].Value = model.po_lastime;
			parameters[10].Value = model.po_sn;
			parameters[11].Value = model.po_ext1;
			parameters[12].Value = model.po_ext2;
			parameters[13].Value = model.po_ext3;
			parameters[14].Value = model.po_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(decimal po_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Tab_Power ");
			strSql.Append(" where po_id=@po_id");
			SqlParameter[] parameters = {
					new SqlParameter("@po_id", SqlDbType.Decimal)
			};
			parameters[0].Value = po_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string po_idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Tab_Power ");
			strSql.Append(" where po_id in ("+po_idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZY.Model.Tab_Power GetModel(decimal po_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 po_id,po_name,po_type,po_ico,po_state,po_src,po_isroot,po_upid,po_note,po_createtime,po_lastime,po_sn,po_ext1,po_ext2,po_ext3 from Tab_Power ");
			strSql.Append(" where po_id=@po_id");
			SqlParameter[] parameters = {
					new SqlParameter("@po_id", SqlDbType.Decimal)
			};
			parameters[0].Value = po_id;

			ZY.Model.Tab_Power model=new ZY.Model.Tab_Power();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZY.Model.Tab_Power DataRowToModel(DataRow row)
		{
			ZY.Model.Tab_Power model=new ZY.Model.Tab_Power();
			if (row != null)
			{
				if(row["po_id"]!=null && row["po_id"].ToString()!="")
				{
					model.po_id=decimal.Parse(row["po_id"].ToString());
				}
				if(row["po_name"]!=null)
				{
					model.po_name=row["po_name"].ToString();
				}
				if(row["po_type"]!=null && row["po_type"].ToString()!="")
				{
					model.po_type=int.Parse(row["po_type"].ToString());
				}
				if(row["po_ico"]!=null)
				{
					model.po_ico=row["po_ico"].ToString();
				}
				if(row["po_state"]!=null && row["po_state"].ToString()!="")
				{
					model.po_state=int.Parse(row["po_state"].ToString());
				}
				if(row["po_src"]!=null)
				{
					model.po_src=row["po_src"].ToString();
				}
				if(row["po_isroot"]!=null && row["po_isroot"].ToString()!="")
				{
					model.po_isroot=int.Parse(row["po_isroot"].ToString());
				}
				if(row["po_upid"]!=null && row["po_upid"].ToString()!="")
				{
					model.po_upid=int.Parse(row["po_upid"].ToString());
				}
				if(row["po_note"]!=null)
				{
					model.po_note=row["po_note"].ToString();
				}
				if(row["po_createtime"]!=null && row["po_createtime"].ToString()!="")
				{
					model.po_createtime=DateTime.Parse(row["po_createtime"].ToString());
				}
				if(row["po_lastime"]!=null && row["po_lastime"].ToString()!="")
				{
					model.po_lastime=DateTime.Parse(row["po_lastime"].ToString());
				}
				if(row["po_sn"]!=null && row["po_sn"].ToString()!="")
				{
					model.po_sn=int.Parse(row["po_sn"].ToString());
				}
				if(row["po_ext1"]!=null)
				{
					model.po_ext1=row["po_ext1"].ToString();
				}
				if(row["po_ext2"]!=null)
				{
					model.po_ext2=row["po_ext2"].ToString();
				}
				if(row["po_ext3"]!=null)
				{
					model.po_ext3=row["po_ext3"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select po_id,po_name,po_type,po_ico,po_state,po_src,po_isroot,po_upid,po_note,po_createtime,po_lastime,po_sn,po_ext1,po_ext2,po_ext3 ");
			strSql.Append(" FROM Tab_Power ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" po_id,po_name,po_type,po_ico,po_state,po_src,po_isroot,po_upid,po_note,po_createtime,po_lastime,po_sn,po_ext1,po_ext2,po_ext3 ");
			strSql.Append(" FROM Tab_Power ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Tab_Power ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.po_id desc");
			}
			strSql.Append(")AS Row, T.*  from Tab_Power T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Tab_Power";
			parameters[1].Value = "po_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

